require('../css/register.less');
require('../fonts/iconfont.css');
document.ready(function () {

    const CaptchaMini = require('../lib/captcha-mini');

    // 获取dom
    let tellInp = document.querySelector('#tel');
    let verifyInp = document.querySelector('#verify');
    let pwdInp = document.querySelector('#pwd');
    let pwdtwoInp = document.querySelector('#pwdTwo');
    let beginInp = document.querySelector('.begin');
    let commomBox = document.querySelector(".box");
    let boxP = document.querySelector(".boxP");
    let icon = document.querySelector('.box div')
    let commomBox1 = document.querySelector(".box1");
    let boxP1 = document.querySelector(".boxP1");
    let icon1 = document.querySelector('.box1 div')
    let body = document.querySelector('body');
    let yzm = '';
    // 调用captcha-mini 
    let captcha1 = new CaptchaMini({
        lineNum: 6,
    });
    captcha1.draw(document.querySelector('#captcha'), function (res) {
        yzm = res.toLowerCase();
    });
    // 注册登录事件
    beginInp.addEventListener('click', function (ev) {

        // 验证手机号
        if (!utils.testTell(tellInp.value)) {
            icon1.style.display = 'block'
            boxP1.innerHTML = '请输入正确的手机号'
            commomBox1.classList.add('commomBox');
            setTimeout(function () {
                commomBox1.classList.remove('commomBox');
                icon1.style.display = 'none'
                boxP1.innerHTML = ''
            }, 2000);
            return;
        }
        // 确定验证码是否正确
        if (yzm != verifyInp.value.toLowerCase()) {
            icon1.style.display = 'block'
            boxP1.innerHTML = '验证码输入错误'
            commomBox1.classList.add('commomBox');
            setTimeout(function () {
                commomBox1.classList.remove('commomBox');
                icon1.style.display = 'none'
                boxP1.innerHTML = ''
            }, 2000);
            return;
        }
        // 确定两次密码是否一致
        if (pwdInp.value != pwdtwoInp.value) {
            icon1.style.display = 'block'
            boxP1.innerHTML = '两次密码输入不一致'
            commomBox1.classList.add('commomBox');
            setTimeout(function () {
                commomBox1.classList.remove('commomBox');
                icon1.style.display = 'none'
                boxP1.innerHTML = ''
            }, 2000);
            return;
        }
        // 判断用户是否全部输完
        if (!(tellInp.value || verifyInp.value || pwdtwoInp.value)) {
            icon1.style.display = 'block'
            boxP1.innerHTML = '请填写完整'
            commomBox1.classList.add('commomBox');
            setTimeout(function () {
                commomBox1.classList.remove('commomBox');
                icon1.style.display = 'none'
                boxP1.innerHTML = ''
            }, 2000);
        } 
        
        // 注册成功自动登录首页


            let data = {
                account: tellInp.value,
                password: pwdInp.value
            };
            $http.post('/users/add', data, function (res) {
                // 判断是否注册成功
                if (res.status===0) {
                    icon.style.display = 'block'
                    boxP.innerHTML = '注册成功'
                    commomBox.classList.add('commomBox');
                    setTimeout(function () {
                        $http.post('/users/login', data, function (res1) {
                            let user = JSON.stringify(res1.data.user);
                            localStorage.setItem('user', user);
                        });
                        location.href = './home.html'
                    }, 2000)
                  }else{
                    icon1.style.display = 'block'
                    boxP1.innerHTML = '注册失败'
                    commomBox1.classList.add('commomBox');
                    setTimeout(function () {
                        commomBox1.classList.remove('commomBox');
                        icon1.style.display = 'none'
                        boxP1.innerHTML = ''
                    }, 2000)
                  }
            })

    });
})